//
//  CountryList.h
//  LPlates.app
//
//  Created by Andreas Schwelling on 23.11.07.
//	http://www.andi.de/licenseplates/
//
//  Released under the GNU General Public License.
//  See LICENSE for details.
//
//  Use at your own risk.



#import "CountryList.h"

@implementation CountryList

- (id)initWithFrame:(struct CGRect)frame
{
	self = [super initWithFrame:frame];
	
	td = [MobileVehicleRegPlates sharedInstance];
	
	
	//Get full screen app rect
	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
	rect.origin.x = rect.origin.y = 0.0f;
	
	navBar = [[UINavigationBar alloc] init];
	[navBar setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 45.0f)];
	[navBar showLeftButton:@"HowTo" withStyle:0 rightButton:@"About" withStyle:0];
	
	
	[navBar setDelegate:self];
	[navBar enableAnimation];
	[navBar pushNavigationItem: [[UINavigationItem alloc] initWithTitle: @"License Plates"]];
	
	bottomNavBar = [[UINavigationBar alloc] init];
	[bottomNavBar setFrame:CGRectMake(rect.origin.x, 415.0f, rect.size.width, 45.0f)];
	[bottomNavBar showLeftButton:@"Update" withStyle:0 rightButton:@"Edit" withStyle:0];
	[bottomNavBar setDelegate:self];
	[bottomNavBar enableAnimation];
	
	
	
	[self buildTable];
	[self addSubview:navBar];
	[self addSubview:bottomNavBar];
	
	
	return self;
}


-(void)fillTable
{
	
	[_tableCells release];
	_tableCells = [[NSMutableArray alloc] init];
	
	NSMutableArray *hideCountries = [[NSMutableArray alloc] init];
	[hideCountries addObjectsFromArray:[td getPListParam:@"hiddenCountries"]];
	countriesarr =  [[NSMutableArray alloc] init];
	
	NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: @"/Applications/LPlates.app/default.plist"];
	
	NSArray *keys,*keys1;
	int k,j, count,count1;
	id key, value,  value1;
	
	keys = [dict allKeys];
	count = [keys count];
	for (j = 0; j < count; j++)
	  {
		  key = [keys objectAtIndex: j];
		  value = [dict objectForKey: key];
		  
		  keys1 = [value allKeys];
		  count1 = [keys1 count];
		  
		  for (k=0;k< count1;k++) {
			  id key1 = [keys1 objectAtIndex: k];
			  value1 = [dict objectForKey: key1];
			  // NSLog (@"Country: %@", key1);
			  [countriesarr addObject:key1];
			  
		  }
		  
		  NSDictionary *inner = [dict objectForKey: @"Countries"];
		  NSDictionary *inner1 = [inner objectForKey: @"Germany"];
		  //   NSLog (@"Key: %@ ", [inner1 allKeys] );
		  
	  }
	
	int i = 0;
	
	
	[countriesarr sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
	for (i = 0;i < [countriesarr count];i++) {
		if ([hideCountries containsObject:[countriesarr objectAtIndex:i]]) continue;
		UIImageAndTextTableCell* cell = [[UIImageAndTextTableCell alloc] init];
		[cell setTitle: [countriesarr objectAtIndex:i]];
		[cell setDisclosureStyle:2];
		
		
		
		
		[cell setShowDisclosure:YES animated:NO];
		[_tableCells addObject: cell]; 
		[cell release];
		
		
	}
	[countriesarr dealloc];
	[_tableHeaders release];
	
	_tableHeaders = [[NSMutableArray alloc] init];
	
	// Each "header cell" here is a dictionary with two keys:
	// title points to a NSSTring of what we want the header to say
	// beginRow is a NSNumber integer that dictates which row this header will come before
	NSMutableDictionary* cellDict = [[NSMutableDictionary alloc] initWithCapacity:2];
	[cellDict setObject:@"Countries" forKey:@"title"];
	[cellDict setObject:[[NSNumber alloc] initWithInt:0] forKey:@"beginRow"];
	
	[_tableHeaders addObject: cellDict];
	
	//	cellDict = [[NSMutableDictionary alloc] initWithCapacity:2];
	//[cellDict setObject:@"B" forKey:@"title"];
	//[cellDict setObject:[[NSNumber alloc] initWithInt:10] forKey:@"beginRow"];
	
	//[_tableHeaders addObject: cellDict];
	[hideCountries release];
	
	
}

// Build the Table
- (void)buildTable
{
	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
	rect.origin.x = rect.origin.y = 0.0f;
	
	
	sectionList = [[UISectionList alloc] initWithFrame:CGRectMake(0.0f, 45.0f, 320.0f, 415.0f-45.0f) showSectionIndex:NO];
	[sectionList setDataSource:self];
	[sectionList reloadData];
	[self addSubview:sectionList];
	
	table = [sectionList table];
	[table setShouldHideHeaderInShortLists:NO];
	UITableColumn * packageColumn = [[UITableColumn alloc] initWithTitle:@"Title" identifier:@"title" width:320.0f];
	
	[table addTableColumn:packageColumn];
	[table setSeparatorStyle:1];
	[table setRowHeight:48.0f];
	[table setDelegate:self];
	[table setAllowsReordering:YES];
	
	[self fillTable];
	
	
	//Refresh the fileview table
	[sectionList reloadData];
	
	
	[self addSubview:sectionList];
}


#pragma mark ----------Delegate Methods-----------

- (void)navigationBar:(UINavigationBar*)bar buttonClicked:(int)button
{
	if(bar == bottomNavBar) {
		if (button == 1) [td showAskUpdateSheet];
		if (button == 0) {
			
			if (showEditView == NO) {
				[bottomNavBar showLeftButton:@"Update" withStyle:0 rightButton:@"Done" withStyle:1];
				[td showCountryEdit];
				showEditView = YES;
			} else {
				[bottomNavBar showLeftButton:@"Update" withStyle:0 rightButton:@"Edit" withStyle:0];
				[td saveHiddenCountries];
				
				[self reloadCountries];
				
				[td hideCountryEdit];
				showEditView = NO;
			}
			
		}
		return;
	}
	
	if (button == 0) {
		[td showAboutSheet];
	}
	if (button == 1) {
		[td showWhatsSheet];
	}
	
}


- (void) reloadCountries
{
	[self fillTable ];
	[table reloadData];
	[sectionList reloadData];
}

- (int)numberOfSectionsInSectionList:(UISectionList *)aSectionList {
	return [_tableHeaders count];
}

- (NSString *)sectionList:(UISectionList *)aSectionList titleForSection:(int)row {	
	return [[_tableHeaders objectAtIndex:row] objectForKey:@"title"];
}       

- (int)sectionList:(UISectionList *)aSectionList rowForSection:(int)row {
	// this is what determines the placement of the header
	return [[[_tableHeaders objectAtIndex:row] valueForKey:@"beginRow"] intValue];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
{
	return [_tableCells objectAtIndex: row];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
			   reusing: (BOOL) reusing
{
	//What does this message do?
    return nil;
}

- (int) numberOfRowsInTable: (UISectionTable*)table
{
	return [_tableCells count];
}

- (void) tableRowSelected: (NSNotification*) notification 
{
	//Get selected cell and filename
	UIImageAndTextTableCell* selectedCell = [_tableCells objectAtIndex: [table selectedRow]];
	NSLog(@"seletedCell title: %@",[selectedCell title]);
	
	//Do stuff here...
	[td setCountry:[selectedCell title]];
	[td showCounties:1];
}

@end